package com.huiquan.medicine.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huiquan.framework.base.BaseController;
import com.huiquan.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.medicine.service.DrugService;

/**
 * 药品 controller
 *
 * @author leo
 * @time 2017年4月5日 下午3:20:12
 */
@Controller
@RequestMapping("/medicine")
public class DrugController extends BaseController {

	@Autowired
	private DrugService drugService;

	/**
	 * 获取药品列表
	 *
	 * @param tabid
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/drug_list.htm")
	public ModelAndView drugList(String tabid){
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "codeSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "countPerPage");

		Map<String, Object> map = drugService.getList(start, keyword,exactSearchFlag,countPerPageStr);
		return new ModelAndView("medicine/drug_list", map);

	}

	@RequestMapping(value = "/drug_pre_edit.htm")
	public ModelAndView preEdit(){
		String codeSearch = RequestParamUtil.getTrimString(getRequest(), "codeSearch");
		String nameSearch = RequestParamUtil.getTrimString(getRequest(), "nameSearch");
		String categorySearch = RequestParamUtil.getTrimString(getRequest(), "categorySearch");
		String exactSearchFlag = RequestParamUtil.getTrimString(getRequest(), "exactSearchFlag");
		String modifyFlag = RequestParamUtil.getTrimString(getRequest(), "modifyFlag");
		String id = getRequestValue("id");
		User user = (User) getSessionValue("user");

		Map<String, Object> map = drugService.preEdit(codeSearch, nameSearch, categorySearch, id, user, exactSearchFlag,
				modifyFlag);
		return new ModelAndView("medicine/drug_edit", map);
	}
	
	/**
	 * 新增或者修改药品
	 *
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/drug_edit.do")
	public ReturnData editDrugImage(MultipartRequest multipartRequest) throws IOException {
		String id = getRequestValue("id");
		User user = (User) getSessionValue("user");
		//新增
		Map<String , MultipartFile> fileMap = new HashMap<>();
		Map<String, String > params = new HashMap<>();
		//修改
		int count = Integer.parseInt(getRequestValue("count"));
		for(int i= 1 ;i<=count;i++){
			fileMap.put("orImage_"+i, multipartRequest.getFile("orImage_"+i));
			fileMap.put("thImage_"+i, multipartRequest.getFile("thImage_"+i));
		}
		params = getParam(getRequest(), params);
		if(StringUtils.isBlank(id)){
			return drugService.addDrug(params, user, fileMap);
		}else {
			//新增
			return drugService.updateDrug(params, user, fileMap,id);
		}
	}
	
	/**
	 * 新增或者修改药品
	 *
	 * @param id
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/delete_drug.do")
	public ReturnData deleteDrug(String id ) {
		return drugService.deleteDrug(id);
	}
	
	/**
	 * 删除图片
	 *
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/delete_drug_image.do")
	public ReturnData deleteDrugImage() throws IOException {
		String id = getRequestValue("id");
		String imageName = getRequestValue("image");
		User user = (User) getSessionValue("user");
		return drugService.deleteImage(id,imageName,user);
		
	}
	private Map<String , String> getParam(HttpServletRequest req,Map<String , String > params){
		params.put("drugName",StringUtils.isNotBlank( req.getParameter("drugName"))? req.getParameter("drugName"):"");
		params.put("produceAddr", StringUtils.isNotBlank( req.getParameter("produceAddr"))? req.getParameter("produceAddr"):"");
		params.put("syrq", StringUtils.isNotBlank( req.getParameter("syrq"))? req.getParameter("syrq"):"");
		params.put("price", StringUtils.isNotBlank( req.getParameter("price"))? req.getParameter("price"):"");
		params.put("buyCity", StringUtils.isNotBlank( req.getParameter("buyCity"))? req.getParameter("buyCity"):"");
		params.put("prescriptionFlag", StringUtils.isNotBlank( req.getParameter("prescriptionFlag"))? req.getParameter("prescriptionFlag"):"");
		params.put("supportInsurance", StringUtils.isNotBlank( req.getParameter("supportInsurance"))? req.getParameter("supportInsurance"):"");
		params.put("sfxy", StringUtils.isNotBlank( req.getParameter("sfxy"))? req.getParameter("sfxy"):"");
		params.put("pharmacyId", StringUtils.isNotBlank( req.getParameter("pharmacyId"))? req.getParameter("pharmacyId"):null);
		params.put("buyFlag", StringUtils.isNotBlank( req.getParameter("buyFlag"))? req.getParameter("buyFlag"):"");
		params.put("component", StringUtils.isNotBlank( req.getParameter("component"))? req.getParameter("component"):"");
		params.put("drugUsage", StringUtils.isNotBlank( req.getParameter("drugUsage"))? req.getParameter("drugUsage"):"");
		params.put("indFun", StringUtils.isNotBlank( req.getParameter("indFun"))? req.getParameter("indFun"):"");
		params.put("specification", StringUtils.isNotBlank( req.getParameter("specification"))? req.getParameter("specification"):"");
		params.put("adverseEffect", StringUtils.isNotBlank( req.getParameter("adverseEffect"))? req.getParameter("adverseEffect"):"");
		params.put("specialUse", StringUtils.isNotBlank( req.getParameter("specialUse"))? req.getParameter("specialUse"):"");
		params.put("taboo", StringUtils.isNotBlank( req.getParameter("taboo"))? req.getParameter("taboo"):"");
		params.put("drugInteraction", StringUtils.isNotBlank( req.getParameter("drugInteraction"))? req.getParameter("drugInteraction"):"");
		params.put("yfyy", StringUtils.isNotBlank( req.getParameter("yfyy"))? req.getParameter("yfyy"):"");
		params.put("etyy", StringUtils.isNotBlank( req.getParameter("etyy"))? req.getParameter("etyy"):"");
		params.put("lnyy", StringUtils.isNotBlank( req.getParameter("lnyy"))? req.getParameter("lnyy"):"");	
		params.put("count", StringUtils.isNotBlank( req.getParameter("count"))? req.getParameter("count"):"");
		params.put("status", StringUtils.isNotBlank( req.getParameter("status"))? req.getParameter("status"):null);	
		params.put("approvalDoc", StringUtils.isNotBlank( req.getParameter("approvalDoc"))? req.getParameter("approvalDoc"):"");


		return params;
	}
}
